//
//  DWJStatusPictureView.swift
//  weibo
//
//  Created by 代伟佳 on 16/4/21.
//  Copyright © 2016年 代伟佳. All rights reserved.
//

import UIKit

private let DWJStatusPictureViewCellID = "DWJStatusPictureViewCell"

// cell的间距
private let itemMargin: CGFloat = 5

// cell 的宽高
private let cellHW: CGFloat = (kWIDTH - 20 - itemMargin * 2) / 3

class DWJStatusPictureView: UICollectionView {

    
    
    var pictureInfo: [DWJPictureInfo]? {
    
        didSet{
        
            let size = getPictureViewSize(pictureInfo?.count ?? 0)
            
            countLabel.text = String(pictureInfo?.count ?? 0)
            
            // 更新约束
            self.snp_updateConstraints { (make) -> Void in
                
                make.size.equalTo(size)
            }
            
            // 得到数据后就舒心
            self.reloadData()
        }
    }
    
    // 处理配图size 
    func getPictureViewSize(count: Int) -> CGSize {

        // 通过有多少张配图 得到配图的尺寸
        
        // 列 行  数
        let col = count >= 3 ? 3 : count
        let row = (count - 1) / 3 + 1
       
        // 配图的宽高
        let width = CGFloat(col) * cellHW + itemMargin * CGFloat(col - 1)
        let height = CGFloat(row) * cellHW + itemMargin * CGFloat(row - 1)
        
        
        return CGSize(width: width, height: height)
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: -  设置视图
    private func setUpUI() {
    
        backgroundColor = UIColor.clearColor()
        
        dataSource = self
        registerClass(DWJStatusPictureViewCell.self, forCellWithReuseIdentifier: DWJStatusPictureViewCellID)
        
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: cellHW, height: cellHW)
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        
        addSubview(countLabel)
        
        
        countLabel.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
    }
    
    private lazy var countLabel: UILabel = {
    
        let lab = UILabel(textColor: UIColor.redColor(), textFont: 20)
        
        return lab
    }()
    
}
extension DWJStatusPictureView: UICollectionViewDataSource{

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pictureInfo?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(DWJStatusPictureViewCellID, forIndexPath: indexPath) as! DWJStatusPictureViewCell

        cell.pic = pictureInfo![indexPath.item]
        
        return cell
    }
    
    
}


class DWJStatusPictureViewCell: UICollectionViewCell {
    
    var pic: DWJPictureInfo? {
    
        didSet{
            
//            print(pic?.thumbnail_pic)
        
            bgImageView.sd_setImageWithURL(NSURL(string: pic?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_card_top_background"))
            
            // 是否是gif 网址后缀为.gif就是动图 否则就不是动图
            if let p = pic?.thumbnail_pic where p.hasPrefix(".gif"){
            
                gifImageView.hidden = false
            } else {
            
                gifImageView.hidden = true
            }
        }
    }
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    
    func setUpUI() {
        
        self.contentView.backgroundColor = UIColor.whiteColor()
        
        
        contentView.addSubview(bgImageView)
        contentView.addSubview(gifImageView)
    
        bgImageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
        gifImageView.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(contentView)
            make.right.equalTo(contentView)
        }
        
    }
    
    
    private lazy var bgImageView: UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "timeline_card_top_background"))
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    
    private lazy var gifImageView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
        return imageView
    }()
}